package com.yisu.auction.api.core.service.impl;

import com.yisu.auction.api.core.mapper.OfferPriceLogArchiveMapper;
import com.yisu.auction.api.core.mapper.OfferPriceLogMapper;
import com.yisu.auction.api.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.api.core.mapper.PrizeMapper;
import com.yisu.auction.api.core.service.IArchiveFileService;
import com.yisu.auction.api.domain.request.ArchiveFileRequest;
import com.yisu.auction.ewf.entity.OfferPriceLog;
import com.yisu.auction.ewf.entity.Prize;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * author: 赵杰
 * Business Description：归档业务实现
 * createTime：2017/9/11
 */
@Service("IArchiveFileService")
public class ArchiveFileServiceImpl implements IArchiveFileService {
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private OfferPriceLogMapper offerPriceLogMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private OfferPriceLogArchiveMapper offerPriceLogArchiveMapper;

    /**
     * author: 赵杰
     * Business Description：归档竞品资源
     *
     * @return
     */
    @Override
    public boolean archivePrizeData(ArchiveFileRequest archiveFileRequest) {
        List<Prize> archivePrizeVos = prizeMapper.getArchivePrizeList(archiveFileRequest);
        if (!CollectionUtils.isEmpty(archivePrizeVos)) {
            for (Prize prize : archivePrizeVos) {
                prizeArchiveMapper.insertSelective(prize);
                List<OfferPriceLog> offerPriceLogs = offerPriceLogMapper.getOfferPriceLogByPrizeId(prize.getId());
                if (!CollectionUtils.isEmpty(offerPriceLogs)) {
                    for (OfferPriceLog item : offerPriceLogs) {
                        offerPriceLogArchiveMapper.insertSelective(item);
                        offerPriceLogMapper.deleteByPrimaryKey(item.getId());
                    }
                }
                prizeMapper.deleteByPrimaryKey(prize.getId());
                System.out.println("==========================================归档竞品,正在进行:" + prize.getId() + "\r\n");
            }
            System.out.println("==========================================归档竞品改批执行完成"  + "\r\n");
        }
        return true;
    }

    /**
     * author: 赵杰
     * Business Description：通过时间获取
     *
     * @param archiveTime
     * @return
     */
    @Override
    public Integer getArchivePrizeCountByArchiveTime(String archiveTime) {
        /**
         * 方案试用调整： JOB服务每隔半小时调用服务，将所有已经开奖的竞品全部归档；后面往期列表冲归档中的查询
         */
        return prizeMapper.getArchivePrizeCountByArchiveTime(archiveTime);
    }
}
